<template>
  <div>
    <basic-page-vxe
      ref="basePage"
      list-api="findSysProdOrgByCondition"
      del-api="deleteSysProdOrgById"
      :search-form="basicData.searchForm"
      :search-handle="basicData.searchHandle"
      :columns="basicData.columns"
    />
    <el-dialog :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="460px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="90px" style="width: 100%">
        <el-form-item label="生产单位" prop="selectValue">
          <el-select v-model="selectValue" filterable placeholder="请选择" style="width: 300px;" value-key="ORG_ID">
            <el-option
              v-for="(item) in orgOption"
              :key="item.ORG_ID"
              :label="item.ORG_NAME"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSysProdOrg, updateSysWell } from '@/api/base'
import axios from 'axios'
export default {
  name: '',
  components: {
  },
  directives: {},
  filters: {},
  data() {
    return {
      basicData: {
        columns: [
          { config: { title: '生产单位Id', field: 'id' }, type: 'copy' },
          { config: { title: '生产单位', field: 'prodOrgName' }, type: 'copy' },
          { config: { title: '机构ID', field: 'prodOrgId' }, type: 'copy' },
          { config: { title: '级别', field: 'prodOrgLevel' }},
          { config: { title: '代码', field: 'prodOrgCode' }},
          { config: { title: '创建日期', width: '160', field: 'createTime' }, type: 'time' },
          { config: { title: '操作', field: 'opt', width: '90' }, type: 'render',
            render: (h, scope) => {
              return (
                <div>
                  <el-button type='danger' v-permission="'delete'" plain size='mini' onClick={() => this.$refs.basePage.handleDelete(scope.row)}><i class='el-icon-delete'></i> 删除</el-button>
                </div>
              )
            }
          }
        ],
        searchData: {
          wellName: null,
          currentPage: 1,
          pageSize: 10
        },
        searchForm: [
        ],
        searchHandle: [
          { label: '新增', type: 'success', icon: 'el-icon-plus', permission: 'add', handle: () => { this.handleCreate() } }
        ]

      },
      showDel: false,
      delInfo: {},
      typ: [],
      detail: {},
      roleIdOptions: [],
      orgIdOptions: [],
      orgName: '',
      temp: {
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        // selectValue: [{ required: true, message: '请选择生产单位', trigger: ['blur', 'change'] }]
      },
      downloadLoading: false,
      selectValue: {},
      orgOption: []
    }
  },
  computed: {},
  created() {
    axios('/xml/org.xml').then((res) => {
      const xml = this.$x2js.xml2js(res.data)
      const arr = xml.DataTable.diffgram.DocumentElement.data
      this.orgOption = arr.filter((item) => { return item.ORG_GRADE === '10' })
      console.log(this.orgOption)
    })
  },
  mounted() {
  },
  methods: {
    // 业务逻辑
    resetTemp() {
      this.temp = {
        id: undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.orgName = ''
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      const params = {
        prodOrgId: this.selectValue.ORG_ID,
        prodOrgLevel: this.selectValue.ORG_GRADE,
        prodOrgName: this.selectValue.ORG_NAME,
        prodParentId: this.selectValue.PARENT_ID,
        prodOrgCode: this.selectValue.ORG_CODE
      }
      addSysProdOrg(params).then(response => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
        this.handleReset()
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // const tempData = Object.assign({}, this.temp)
          const tempData = {}
          tempData.id = this.temp.id
          tempData.wellName = this.temp.wellName
          tempData.wellAreaName = this.temp.wellAreaName
          tempData.wellOrgName = this.temp.wellOrgName
          tempData.wellProject = this.temp.wellProject
          tempData.displayOrder = this.temp.displayOrder
          updateSysWell(tempData).then(response => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.handleReset()
          })
        }
      })
    },
    showDelDialog(row) {
      this.delInfo = row
      this.showDel = true
    },
    // 刷新
    handleReset() {
      this.$refs.basePage.getList()
    }
  }
}
</script>

<style scoped>
.warning{
  text-align: center;
  font-size: 20px;
}
.el-icon-info{
  font-size: 30px;
  color: orange;
}
</style>
